package com.example.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class ChangePasswordActivity extends AppCompatActivity {

    private EditText etCurrentPassword, etNewPassword, etConfirmPassword;
    private Button btnChangePassword;
    private String accountId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_change_password);

        // 获取传递的账号ID
        accountId = getIntent().getStringExtra("account_id");
        if (accountId == null) {
            Toast.makeText(this, "无效的账号", Toast.LENGTH_SHORT).show();
            finish();
        }

        initViews();
        setupListeners();
    }

    private void initViews() {
        etCurrentPassword = findViewById(R.id.etCurrentPassword);
        etNewPassword = findViewById(R.id.etNewPassword);
        etConfirmPassword = findViewById(R.id.etConfirmPassword);
//        btnChangePassword = findViewById(R.id.btnChangePassword);
    }

    private void setupListeners() {
        btnChangePassword.setOnClickListener(v -> attemptChangePassword());
    }

    private void attemptChangePassword() {
        String currentPassword = etCurrentPassword.getText().toString().trim();
        String newPassword = etNewPassword.getText().toString().trim();
        String confirmPassword = etConfirmPassword.getText().toString().trim();

        if (!validateInputs(currentPassword, newPassword, confirmPassword)) {
            return;
        }

        // 模拟密码修改逻辑（实际项目应使用加密存储）
        if (updatePasswordInPrefs(currentPassword, newPassword)) {
            Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK);
            finish();
        } else {
            Toast.makeText(this, "当前密码不正确", Toast.LENGTH_SHORT).show();
        }
    }

    private boolean validateInputs(String current, String newPwd, String confirmPwd) {
        if (current.isEmpty()) {
            etCurrentPassword.setError("请输入当前密码");
            return false;
        }
        
        if (newPwd.isEmpty() || newPwd.length() < 6) {
            etNewPassword.setError("密码至少需要6位");
            return false;
        }
        
        if (!newPwd.equals(confirmPwd)) {
            etConfirmPassword.setError("两次输入密码不一致");
            return false;
        }
        
        return true;
    }

    private boolean updatePasswordInPrefs(String currentPassword, String newPassword) {
        SharedPreferences prefs = getSharedPreferences("account_prefs", MODE_PRIVATE);
        String savedPassword = prefs.getString("account_" + accountId + "_pwd", "");
        
        // 模拟验证当前密码（实际应使用加密比较）
        if (savedPassword.equals(currentPassword)) {
            prefs.edit()
                .putString("account_" + accountId + "_pwd", newPassword)
                .apply();
            return true;
        }
        return false;
    }
}